package cn.com.cym.gamezao.entity.authorize;

import cn.com.cym.gamezao.annotation.Sensitive;
import cn.com.cym.gamezao.base.BaseEntity;
import cn.com.cym.gamezao.config.SensitiveStrategy;
import cn.com.cym.gamezao.entity.xmu.Recruit;
import cn.com.cym.gamezao.enums.UserType;
import cn.com.cym.gamezao.util.MathUtils;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;

import javax.persistence.*;
import javax.validation.constraints.Pattern;
import java.io.Serializable;
import java.util.Set;


/**
 * 用户管理
 */
@Setter
@Getter
@NoArgsConstructor
@Table(name = "a_user",uniqueConstraints=@UniqueConstraint(columnNames="userName"))
@Entity
@Accessors(chain = true)
@ApiModel(description="游戏用户")
public class User extends BaseEntity implements Serializable {
    private static final long serialVersionUID = -2408112080795306261L;


    @ApiModelProperty(value = "用户名",required = true,dataType = "String")
    @Column(length = 120)
    private String userName;


    @ApiModelProperty(value = "邮箱",dataType = "String")
    @Column(length = 120)
    //@Pattern(regexp = "^(\\w+((-\\w+)|(\\.\\w+))*)\\+\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$")
    private String email;


    /**
     * 设置密码不可json化，前端无法查看密码
     */
    @ApiModelProperty(value = "密码",required = true,dataType = "String")
    @Column(length = 100)
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private String password;


    @ApiModelProperty(value = "wxId微信唯一识别号",dataType = "String")
    @Column(length = 80)
    private String wxId;



    @ApiModelProperty(value = "联系方式",dataType = "String")
    @Pattern(regexp = "^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$")
    @Column(length = 11)
    @Sensitive(strategy = SensitiveStrategy.PHONE)
    private String tel;


    /**
     * 多张子表需要使用set集合来获取
     */
    //@Transient
    @ApiModelProperty(value = "用户信息",dataType = "UserInfo")
    @OneToOne(mappedBy="user",fetch = FetchType.EAGER)
    private UserInfo userInfo;

    //@Transient
    @ApiModelProperty(value = "用户角色",dataType = "RoleUser")
    @OneToMany(mappedBy="user",fetch = FetchType.EAGER)
    private Set<UserRole> userRoles;

    /*@ApiModelProperty(value = "用户角色",dataType = "RoleUser")
    @OneToOne(mappedBy="user",fetch = FetchType.LAZY)
    private Recruit recruit;*/


    //@Transient
    @ApiModelProperty(value = "用户当前登录ip",dataType = "String")
    private String currentIp;


    //用户类型 默认是应聘人员
    private UserType userType=UserType.APPLICANT;

    /**
     * SECRET 简单的生成一个秘钥
     */
    public String setSecret(){
        String s = MathUtils.mathUserName(this.getUserName());
        return s+this.currentIp;
    }
}
